package com.voxel.sense.infomsg.module.publish.personal;

import com.example.com.common.factory.data.DataSource;
import com.example.com.common.utils.NetworkUtils;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.base.RecyclerViewPresenter;

import java.util.List;

import cyy.example.com.facotry.Factory;
import cyy.example.com.facotry.model.api.BaseEntity;
import cyy.example.com.facotry.model.api.MyPublishListEntity;
import cyy.example.com.facotry.net.helper.NetHelper;

/**
 * Created by ChenYiYao on 2018/9/5.
 */
public class MyPublishPresenter extends RecyclerViewPresenter<MyPublishContract.View>
        implements MyPublishContract.Presenter, DataSource.Callback<BaseEntity<List<MyPublishListEntity>>> {


    public MyPublishPresenter(MyPublishContract.View view) {
        super(view);
    }

    @Override
    protected void loadData() {
        NetHelper.getMyPublish(getPAGE_NUMB() , getPageDataSize(), this);
    }

    @Override
    protected void loadMoreData() {
        NetHelper.getMyPublish( getPAGE_NUMB() , getPageDataSize(), this);
    }

    @Override
    public void doLoadMore() {
        if (NetworkUtils.isConnected()) {
            startLoadMoreData();
        } else {
            mView.showError(Factory.app().getString(R.string.network_unavailable));
            LoadDataFailure();
            mView.onLoadMoreFinish();
        }
    }

    @Override
    public void doRefresh() {
        if (NetworkUtils.isConnected()) {
            startRefreshData();
        } else {
            mView.showError(Factory.app().getString(R.string.network_unavailable));
            LoadDataFailure();
            mView.onRefreshFinish();
        }
    }


    @Override
    public void onDataLoaded(BaseEntity<List<MyPublishListEntity>> data, int reqId) {
        if (mView == null) {
            return;
        }

        if (data.isSuccess()) {
            List<MyPublishListEntity> data1 = data.getData();
            loadDataComplete(data1);
        } else {
            LoadDataFailure();
        }

    }

    @Override
    public void onDataNotAvailable(String strRes, int reqId) {
        if (mView == null) {
            return;
        }
        LoadDataFailure();
        mView.showError(Factory.app().getString(R.string.network_error));
    }
}
